//
//  XLScheduleCustomView.m
//  industry
//
//  Created by xl on 2020/12/18.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLScheduleCustomView.h"
#import "LLRPSegment.h"
#import "YQSearchBarView.h"
#import "XLScheduleStatusView.h"
#import "XLScheduleViewProtocol.h"

@interface XLScheduleCustomView ()<RPTaggedNavViewDelegate, XLScheduleStatusViewDelegate>

@property (nonatomic, strong) LLRPSegment *segment;
@property (nonatomic, strong) YQSearchBarView *searchBarView;
@property (nonatomic, strong) XLScheduleStatusView *statuView;
@property (nonatomic, strong) XLView *lineView;

@property (nonatomic, assign) NSInteger currentSegmentIndex;
@property (nonatomic, strong) NSArray *statusArr;

@end
@implementation XLScheduleCustomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.segment];
        [self addSubview:self.searchBarView];
        [self addSubview:self.statuView];
        [self addSubview:self.lineView];
        self.currentSegmentIndex = 0;
        [self makeConstraints];
    }
    return self;
}

- (void)makeConstraints {
    [_segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(STATUSBAR_HEIGHT + 30);
        make.size.mas_equalTo(CGSizeMake(140, 25));
    }];
    
    [_searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segment.mas_bottom).offset(10);
        make.left.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    
    [_statuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchBarView.mas_bottom).offset(10);
        make.left.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.bottom.equalTo(self);
        make.height.mas_offset(1);
    }];
}

#pragma mark - Public
- (void)loadSceduleStatusDataArr:(NSArray *)datasArr {
    self.statusArr = datasArr;
    [_statuView loadItemsDataArr:datasArr];
}

#pragma mark - RPTaggedNavViewDelegate
- (void)haveSelectedIndex:(NSInteger)index {
    [self endEditing:YES];
    if (self.currentSegmentIndex == index) {
        return;
    }
    self.currentSegmentIndex = index;
    if (index == 0) {
        _statuView.hidden = NO;
        [_statuView loadItemsDataArr:self.statusArr];
        [_searchBarView changeTextFieldPlaceHolder:@"搜索待办事项"];
    } else {
        _statuView.hidden = YES;
        [_searchBarView changeTextFieldPlaceHolder:@"搜索已审核事项"];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(segmentChangeIndex:)]) {
        [self.delegate segmentChangeIndex:index];
    }
}

#pragma mark - XLScheduleStatusViewDelegate
- (void)scheduleStatusSelectIndex:(NSInteger)index {
    if (self.delegate && [self.delegate respondsToSelector:@selector(statusChangeIndex:)]) {
        [self.delegate statusChangeIndex:index];
    }
}

#pragma mark - setter/getter
- (LLRPSegment *)segment {
    if (!_segment) {
        _segment = [[LLRPSegment alloc] initWithFrame:CGRectMake(0, 0, 140, 35)];
        _segment.backgroundColor = [UIColor whiteColor];
        _segment.delegate = self;
        _segment.dataArr = [NSArray arrayWithObjects:@"待办事项",@"已审批", nil];
        _segment.sliderColor = [UIColor blueBgColor];
        _segment.tagTextFont_normal = [UIFont fontOfPingFangSCRegularWithSize:14];
        _segment.tagTextFont_selected = [UIFont fontOfPingFangSCSemiboldWithSize:17];
        _segment.tagTextColor_normal = [UIColor grayFontColor];
        _segment.tagTextColor_selected = [UIColor blackFontColor];
        _segment.sliderW = 14;
        _segment.sliderH = 4;
    }
    return _segment;
}

- (YQSearchBarView *)searchBarView {
    if (!_searchBarView) {
        _searchBarView = [[YQSearchBarView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        [_searchBarView changeTextFieldPlaceHolder:@"搜索待办事项"];
        WEAKSELF
        [_searchBarView setTextFieldChangeCallBack:^(NSString * _Nonnull text) {
            XLSend(weakSelf.baseController.cView, XLScheduleViewProtocol, beginSearchBar);
        }];
    }
    return _searchBarView;
}

- (XLScheduleStatusView *)statuView {
    if (!_statuView) {
        _statuView = [[XLScheduleStatusView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        _statuView.delegate = self;
    }
    return _statuView;
}

- (XLView *)lineView {
    if (!_lineView) {
        _lineView = [XLView new];
        _lineView.backgroundColor = [UIColor graySecondLineColor];
    }
    return _lineView;
}

@end
